package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
public class RegisterController
{
    private DvdRegister register;
    
    /** CONSTRUCTORS */
    public RegisterController()
    {
        register = DvdRegister.getInstance();
    }
    
    /** ACCESSORS */
    public DVD findDVD(String dvdId)
    {
        return register.findDVD(dvdId);
    }
    
    public void createDVD(String dvdId, String title, String artist, String publicationDate)
    {
        if(findDVD(dvdId)!=null)
        {
            System.out.println("There is already a DVD with id "+ dvdId +".");
        }
        else
        {
            //creates a DVD object and stores it in the container
            register.addDVD(dvdId, title, artist, publicationDate); 
        }
    }
    
    public void deleteDVD(String dvdId)
    {
        if(findDVD(dvdId)==null){
            System.out.println("Can't delete "+dvdId+" DVD. It doesn't exist");
        }else{
            register.removeDVD(dvdId);
            System.out.println("DVD "+dvdId+" was successfully deleted.");
        }
    }
    
    public void updateDVD(DVD myDVD, String dvdId, String title, String artist, String publicationDate)
    {
        if(findDVD(dvdId)==null){
            System.out.println("Can't update. "+dvdId+" doesn't exist.");
        }else{
        register.updateDVD(myDVD, dvdId, title, artist, publicationDate);
        }
    }
    
    public void listAllDvds()
    {
          if(register.findDVD().isEmpty()){
            System.out.println("There's no list of DVDS at the time");
          }
          int index=0;
        while(index<register.findDVD().size())
        {
            System.out.println(register.findDVD().get(index).toString());
            System.out.println("This DVD has " + register.findDVD().get(index).copiesCollection().size() +" copies");
            int i=0;
            while (i<register.findDVD().get(index).copiesCollection().size())
            {
                int k=i+1;
                System.out.println("Copy nr."+k);
                System.out.println(register.findDVD().get(index).copiesCollection().get(i).toString());
            i++;
            }
            System.out.println("___________________________________________________________");
            
            index++;
        }
    }
    
    public void createCopy(String DVDtitle,String serialNumber, String purchaseDate, String purchasePrice)
    {
        if(findDVD(DVDtitle).findCopy(serialNumber)==null){
         findDVD(DVDtitle).createCopy(serialNumber,purchaseDate, purchasePrice);
        }else{
            System.out.println("Such copy already exists.");
        }
    }
    
    public void updateCopy(String DVDtitle, String serialNumber, String purchaseDate, String purchasePrice){
        if(findDVD(DVDtitle).findCopy(serialNumber)!=null){
            System.out.println("Such copy already exists.");
        }
        findDVD(DVDtitle).update(serialNumber,purchaseDate, purchasePrice);
    }
    
    public void deleteCopy(String DVDtitle, String serialNumber){
        if(findDVD(DVDtitle).findCopy(serialNumber)!=null){
        findDVD(DVDtitle).removeCopy(serialNumber);
        }else{
            System.out.println("Such copy doesn't exists");
        }
    }
    
    public void findCopy(String DVDtitle, String serialNumber){
        if(findDVD(DVDtitle).findCopy(serialNumber)!=null){
        findDVD(DVDtitle).findCopy(serialNumber);
        }else{
            System.out.println("Could not find such copy.");
        }
    }
}
